package com.qianguo.buyer;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;

import com.qianguo.buyer.base.BaseTitleActivity;
import com.qianguo.buyer.constant.BuyerKeys;
import com.qianguo.buyer.entity.EventActionBean;
import com.qianguo.buyer.entity.ShopCarItem;
import com.qianguo.buyer.tab.ShopCartFragment;
import com.qianguo.buyer.tab.ShopCartFragment_;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;



/**
 * 购物车详情页
 *
 * @author guxun
 * @date 2016-06-28 16:43
 */
@EActivity(R.layout.activity_shop_car_detail_main)
public class ShopCarDetailActivity extends BaseTitleActivity {

    @Extra(BuyerKeys.EXTRA_KEY_SHOP_ID)
    String mShopID;
    private ShopCartFragment mShopCarFragment;
    private ArrayList<String> mCarIDs = new ArrayList<>();
    public static void jumpToShopCarDetail(Context context,String shopID) {
        Intent intent = new Intent();
        intent.setClass(context, ShopCarDetailActivity_.class);
        intent.putExtra(BuyerKeys.EXTRA_KEY_SHOP_ID, shopID);
        context.startActivity(intent);
    }

    @Click(R.id.tv_next_step)
    void nextStep(){
        OrderConfirmActivity.jumpToShopCarDetail(this,mCarIDs);
    }

    @Override
    protected String getToolbarTitle() {
        return "购物车";
    }

    @Override
    protected void initializeUI() {
        EventBus.getDefault().register(this);
        mShopCarFragment = new ShopCartFragment_();
        Bundle args = new Bundle();
        args.putString(BuyerKeys.EXTRA_KEY_SHOP_ID,mShopID);
        mShopCarFragment.setArguments(args);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.content_fl, mShopCarFragment);
        transaction.commitAllowingStateLoss();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventActionBean bean) {
        if (BuyerKeys.ACTION_UPDATE_CARIDS.equals(bean.getAction())) {
            mCarIDs.clear();
            for(ShopCarItem goods:((List<ShopCarItem>)bean.obj)){
                mCarIDs.add(String.valueOf(goods.getCartid()));
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
